﻿using NUnit.Framework;
using System;
using System.IO;
using System.Reflection;

namespace CSharp.Libs.Database
{
    [TestFixture]
    public class Test_SQLiteHandler
    {
        [TestFixture]
        public class Constructor
        {
            [Test]
            public void Subtest()
            {
                Assert.IsTrue(true);
            }
        }


        #region declarations
        private string _strFilename = string.Concat((new FileInfo(Assembly.GetExecutingAssembly().Location)).DirectoryName, @"\SQLiteHandler_Test.s3db");
        #endregion



        #region basic tests
        [Test]
        public void TestTest() { Assert.IsTrue(true); }
        #endregion



        #region test Constructors
        [Test]
        [Description("Testing Constructor 1. SQLiteHandler creates database file. An existing file will be deleted first. Connection Mode must be \"AlwaysOpen\"")]
        public void Constructor_1()
        {
            Console.WriteLine("Test-Filename: " + _strFilename);

            //if file exists, delete it
            if (File.Exists(_strFilename))
            {
                File.Delete(_strFilename); 
                Console.WriteLine("Needed to delete existing file (" + _strFilename + ")");
            }

            //if it still exists, something went wrong
            Assert.IsFalse(File.Exists(_strFilename), "Database file still exists.");

            //instanciate SQLiteHandler object
            SQLiteHandler slh = new SQLiteHandler(_strFilename);
            
            Assert.IsTrue(File.Exists(_strFilename), "Database file was not created.");
            Assert.IsTrue(slh.ConnectionMode == ConnectionBehaviour.AllwaysOpen, "ConnectionMode is " + slh.ConnectionMode.ToString());

            //dispose it
            slh.Dispose();
        }

        [Test]
        [Description("Testing Constructor 2. SQLiteHandler creates database file. ConnectionMode is set to \"AutomaticOpenAndClose\"")]
        public void Constructor_2()
        {
            Console.WriteLine("Test-Filename: " + _strFilename);

            //instanciate SQLiteHandler object
            SQLiteHandler slh = new SQLiteHandler(_strFilename, ConnectionBehaviour.AutomaticOpenAndClose);

            Assert.IsTrue(slh.ConnectionMode == ConnectionBehaviour.AutomaticOpenAndClose, "ConnectionMode is " + slh.ConnectionMode.ToString());

            //dispose it
            slh.Dispose();
        }

        [Test]
        [Description("Testing Constructor 2. SQLiteHandler creates database file. ConnectionMode is set to \"AutomaticOpenAndClose\"")]
        public void Constructor_2a()
        {
            Console.WriteLine("Test-Filename: " + _strFilename);

            //instanciate SQLiteHandler object
            SQLiteHandler slh = new SQLiteHandler(_strFilename, ConnectionBehaviour.Manually);

            Assert.IsTrue(slh.ConnectionMode == ConnectionBehaviour.Manually, "ConnectionMode is " + slh.ConnectionMode.ToString());

            //dispose it
            slh.Dispose();
        }
        #endregion



        #region test connection methods
        #endregion



        #region test sql execution methods
        [Test]
        [Description("Testing SQL execution")]
        public void SqlExecution()
        {
            Console.WriteLine("Test-Filename: " + _strFilename);

            //if file exists, delete it
            if (File.Exists(_strFilename))
            {
                File.Delete(_strFilename);
                Console.WriteLine("Needed to delete existing file (" + _strFilename + ")");
            }

            //instanciate SQLiteHandler object
            SQLiteHandler slh = new SQLiteHandler(_strFilename);

            string strSQL = "CREATE TABLE [tblFileSystemObjects] ( [Id] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT, [Hash] VARCHAR(2048)  NULL, [Ignore] BOOLEAN DEFAULT 'FALSE' NULL, [Name] VARCHAR(256)  NULL, [Path] VARCHAR(1024)  NULL)";
            Assert.IsTrue(slh.ExecuteSql(strSQL));

            //TODO: list tables by manually connecting to database and executing "SELECT * FROM sqlite_master WHERE type='table';". Check if tblFileSystemObjects exists.
            /* HERE */
            //TODO: list tables by manually connecting to database and executing "SELECT * FROM sqlite_master WHERE type='table';". Check if tblFileSystemObjects exists.

            //dispose it
            slh.Dispose();
        }
        #endregion
    }
}
